﻿#include "DarkWraith.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/Enums.h"
#include "../../../Shared/Data/Stat.h"
#include "../MapObject.h"
#include "../../../Shared/ServerPackets.h"
#include "../DelayedAction.h"
#include "../../MirEnvir/Map.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    DarkWraith::DarkWraith(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool DarkWraith::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);
    }

    bool DarkWraith::InRangeAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), AttackRange);
    }

    void DarkWraith::ProcessTarget()
    {
        if (getTarget() == nullptr || Dead)
        {
            return;
        }

        if (!InAttackRange())
        {
            if (getCanAttack())
            {
                if (getEnvir()->Random->Next(3) == 0)
                {
                    LineAttack(4);
                }
            }
            if (getCurrentLocation() == getTarget()->getCurrentLocation())
            {
                MirDirection direction = static_cast<MirDirection>(getEnvir()->Random->Next(8));
                int rotation = getEnvir()->Random->Next(2) == 0 ? 1 : -1;

                for (int d = 0; d < 8; d++)
                {
                    if (Walk(direction))
                    {
                        break;
                    }

                    direction = Functions::ShiftDirection(direction, rotation);
                }
            }
            else
            {
                MoveTo(getTarget()->getCurrentLocation());
            }
        }

        if (!getCanAttack())
        {
            return;
        }

        if (getEnvir()->Random->Next(3) > 0)
        {
            if (InAttackRange())
            {
                Attack();
            }
        }
        else
        {
            LineAttack(4);
        }

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }
    }

    void DarkWraith::Attack()
    {

        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (damage == 0)
        {
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));

        std::vector<MapObject*> targets = FindAllTargets(1, getCurrentLocation());

        if (targets.size() > 1 && getEnvir()->Random->Next(2) > 0)
        {
            S::ObjectAttack *tempVar = new S::ObjectAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            tempVar->Type = 1;
            Broadcast(tempVar);
            for (int i = 0; i < targets.size(); i++)
            {
                DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 600, {targets[i], damage, DefenceType::AC});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
            }

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            MonsterObject::Attack();
        }


        ActionTime = getEnvir()->getTime() + 500;
        AttackTime = getEnvir()->getTime() + AttackSpeed;
        ShockTime = 0;


    }

    void DarkWraith::LineAttack(int distance)
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        if (LineAttackTime > getEnvir()->getTime())
        {
            return;
        }
        ShockTime = 0;
        ActionTime = getEnvir()->getTime() + 500;
        AttackTime = getEnvir()->getTime() + AttackSpeed + 500;
        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]) * 3;
        if (damage == 0)
        {
            return;
        }

        LineAttackTime = getEnvir()->getTime() + 3000 + getEnvir()->Random->Next(5) * 1000;
        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        S::ObjectAttack *tempVar = new S::ObjectAttack();
        tempVar->ObjectID = ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        tempVar->Type = 2;
        Broadcast(tempVar);

        for (int i = 1; i <= distance; i++)
        {
            Point *target = Functions::PointMove(getCurrentLocation(), getDirection(), i);

            if (target == getTarget()->getCurrentLocation())
            {
                getTarget()->Attacked(this, damage, DefenceType::ACAgility);
            }
            else
            {
                if (!getCurrentMap()->ValidPoint(target))
                {
                    continue;
                }

                Cell *cell = getCurrentMap()->GetCell(target);
                if (cell->Objects.empty())
                {
                    continue;
                }

                for (int o = 0; o < cell->Objects.size(); o++)
                {
                    MapObject *ob = cell->Objects[o];
                    if (ob->getRace() == ObjectType::Monster || ob->getRace() == ObjectType::Player)
                    {
                        if (!ob->IsAttackTarget(this))
                        {
                            continue;
                        }

                        ob->Attacked(this, damage, DefenceType::AC);
                    }
                    else
                    {
                        continue;
                    }

                    break;
                }

            }
        }

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }
}
